<?php
namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;

class Special extends Permissions
{
	//设备列表
	public function spelist()
	{
		$where = []; 
		if(!empty(input('store_id'))){
			 $where['p.store_id'] = input('store_id');
		}
        if(!empty(input('machine_code'))){
			 $where['p.machine_code'] = input('machine_code');
		}
		if(!empty(input('machine_code1'))){
			 $where['p.machine_code1'] = input('machine_code1');
		}
		$list = db('printer')->alias('p')
		    ->join('user u','p.user_id = u.id','LEFT')
			->join('store s','p.store_id = s.id','LEFT')
			->field('p.*,u.user_name,s.store_name')
		    ->where($where) 
			->paginate(10,false,['query'=>request()->param()]);
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		$this->assign('list',$list);
		$this->assign('store_id',input('store_id'));
		$this->assign('machine_code',input('machine_code'));
		$this->assign('machine_code1',input('machine_code1'));
		return $this->fetch();
	}
	
	//新增设备
	public function speadd()
	{
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		
		if(request()->isPost()){
			$data = $_POST;  
			$res = db('printer')->insert($data);
			if($res){ 
				return $this->success('新增成功','admin/special/spelist');
	        }else{ 
				return $this->error('新增失败');
	        }
		}
		return $this->fetch();
	}
	
	//前台打印机开关
	public function prt_status()
	{
		$data['paid'] = input('paid');
		$id = input('id');  
		$res = db('printer')->where('id',$id)->update($data);
		if(input('paid') == 1){
			$msg = "已关闭";
		}else{
			$msg = "已开启";
		}
	    if($res){
			return json(['code'=>200,'msg'=>$msg]);
        }else{
       		return json(['code'=>400,'msg'=>$msg]);
        }
	}
	
	//传菜部打印机开关
	public function store_switch()
	{
		$data['store_switch'] = input('store_switch');
		$id = input('id');  
		$res = db('printer')->where('id',$id)->update($data);
		if(input('store_switch') == 1){
			$msg = "已关闭";
		}else{
			$msg = "已开启";
		}
	    if($res){
			return json(['code'=>200,'msg'=>$msg]);
        }else{
       		return json(['code'=>400,'msg'=>$msg]);
        }
	}
	
	//设置
	public function speedit()
	{
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		
		//查看设备信息
		$info = db('printer')->where('id',input('id'))->find();
		$this->assign('info',$info);
		
		if(request()->isPost()){
			$data = $_POST;  
			$res = db('printer')->where('id',$_POST['id'])->update($data);
			if($res){ 
				return $this->success('设置成功','admin/special/spelist');
	        }else{ 
				return $this->error('设置失败');
	        }
		}
		return $this->fetch();
	}
}
?>